package tw.com.softleader.fire.setting.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import tw.com.softleader.fire.common.dao.GenericDao;
import tw.com.softleader.fire.setting.entity.AcuEntity;

/**
 * 
 * @author Khalil
 *
 */
@Repository
public class AcuDao extends GenericDao<AcuEntity> {
	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public List<AcuEntity> findByNo(AcuEntity entity) {
		Session session = sessionFactory.getCurrentSession();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT * FROM set_acu WHERE acu_No ILIKE ?");
		Query query = session.createSQLQuery(sql.toString()).addEntity(AcuEntity.class);
		List<AcuEntity> models=null;
		
		if(entity!=null && StringUtils.isEmpty(entity.getAcuNo()) ) {
			entity.setAcuNo("%");
			models = query.setString(0, entity.getAcuNo()).list();	
		}else {
			entity.setAcuNo("%"+entity.getAcuNo()+"%");
			models = query.setString(0, entity.getAcuNo()).list();	
		}
		
		return models;
	}
}
